<?php
class Product extends CI_Controller
{
		function __construct()
			 {
			    parent::__construct();	
			    $this->load->helper(array("url","form"));
		
			 }
			 
	    function index()
	    {
		    	/*     分页代码          */
		    $num = 4;    //每页显示数量
			if($this->uri->segment(3) == FALSE)
			{
				$offset = 0;
			}else{
				if($this->uri->segment(3) ==1)
				{
					$offset=0;
				}else{
				$offset = $this->uri->segment(3)*$num-$num;   //计算篇移量
				} 
			}
			$counts = $this->db->count_all('product');
		    $count_page = ceil($counts/$num);
	        $data['count_page'] = $count_page;  
	       
		    $this->db->select('*');
		    $this->db->limit($num, $offset);   
		    $this->db->order_by('pid','desc');
	
		    $data['products'] = $this->db->get('product')->result_array();
		    
		    $this->load->view('manage/header');
		    $this->load->view('product/index',$data);
		    $this->load->view('manage/footer');
	    }

	    function post()
	    {
			    $this->load->library('form_validation');
				
				$this->form_validation->set_rules('product_name','product_name','required');
				if($this->form_validation->run() == FALSE)
				{
					$this->db->select('*');
					$query = $this->db->get('product_cat');
					$cate = $query->result_array();
					$data['cate'] = $cate;
					$this->load->view('manage/header');
					$this->load->view('product/post',$data);
					$this->load->view('manage/footer');
				}else{
					$this->load->model('model_product',"",TRUE);
					
					$this->model_product->save();
					redirect("product/post", "refresh");
					//$this->load->view('success');
				}
	    }
	    
	    function edit()
	    {
	    	$this->load->library('form_validation');
	    	
	    	$pid = $this->uri->segment(3);
	    	
	    		$this->form_validation->set_rules('product_name','product_name','required');
	    		if($this->form_validation->run() == false)
	    		{
	    			$pid = intval($pid);
	    			$product = $this->db->where('pid',$pid)->get('product')->result_array();
                    $data['product'] = $product[0];
	    			$this->db->select('*');
					$data['cate'] = $this->db->get('product_cat')->result_array();
					$this->load->view('manage/header');
					$this->load->view('product/edit',$data);
					$this->load->view('manage/footer');	    			
	    		}else{
	    			$this->load->model('model_product',"",TRUE);
	    			$this->model_product->update();
	    			redirect('product/index');
	    		}
	    	
	    }
	    
	    function drop()
	    {
	    	$pid = $this->uri->segment(3);
	    	if($pid){
	    	    $pid = intval($pid);
	    	    $this->db->where('pid',$pid)->delete('product');
	    
	    	}
	    	redirect('product/index');
	    }
		
		function activate()
		{
		    $pid = $this->uri->segment(3);
			if($pid)
			{
			    $pid = intval($pid);
				$data = array('is_show'=>1);
				$this->db->where('pid',$pid)->update('product',$data);
			}
			redirect('product/index');
		}
		
		function batch_activate()
		{
		    $pids = $this->input->post('cbSelect');
			if($pids)
			{
			    for($i=0;$i<count();$i++)
				{
					$pid = intval($pids[$i]);
					$data = array('is_show'=>$pid);
					$this->db->where('pid',$pid)->update('product',$data);
				}
			}
			redirect('product/index');
		}
		
		function category()
		{
		    	/*     分页代码          */
		    $num = 4;    //每页显示数量
			if($this->uri->segment(3) == FALSE)
			{
				$offset = 0;
			}else{
				if($this->uri->segment(3) ==1)
				{
					$offset=0;
				}else{
				$offset = $this->uri->segment(3)*$num-$num;   //计算篇移量
				} 
			}
			$counts = $this->db->count_all('product_cat');
		    $count_page = ceil($counts/$num);
	        $data['count_page'] = $count_page;  
	       
		    $this->db->select('*');
		    $this->db->limit($num, $offset);   
		    $this->db->order_by('id','desc');
	
		    $data['products'] = $this->db->get('product_cat')->result_array();
		    
		    $this->load->view('manage/header');
		    $this->load->view('product/category',$data);
		    $this->load->view('manage/footer');
		}
		
		function cat_post()
		{
		    $this->load->library('form_validation');
				
				$this->form_validation->set_rules('cat_name','cat_name','required');
				if($this->form_validation->run() == FALSE)
				{
					$this->db->select('*');
					$query = $this->db->get('product_cat');
					$cate = $query->result_array();
					$data['cate'] = $cate;
					
					$this->load->helper('category');
		            $data['categorys']  = $this->get_category_tree(0);
					
					$this->load->view('manage/header');
					$this->load->view('product/cat_post',$data);
					$this->load->view('manage/footer');
				}else{
					$this->load->model('model_product',"",TRUE);
					
					$this->model_product->cat_save();
					redirect("product/category", "refresh");
					//$this->load->view('success');
				}
		}
		
		function cat_edit()
		{
		   $this->load->library('form_validation');
		   
		   $this->form_validation->set_rules('cat_name','cat_name','required');
		   if($this->form_validation->run() == false)
		   {
			   $this->load->helper('category');
			   $data['categorys']  = $this->get_category_tree(0);
			   $id = $this->uri->segment(3);
			   if(false == $id)
			   {
			        redirect('product/category');
			   }
			   $id = intval($id);
			   $res = $this->db->where('id',$id)->get('product_cat')->result_array();
			   $data['product_cat'] = $res[0];
			   $this->load->view('manage/header');
			   $this->load->view('product/cat_edit',$data);
			   $this->load->view('manage/footer');
		   }else{
		       $this->load->model('model_product','',true);
			   $this->model_product->cat_update();
			   redirect('product/category');
		   }
		}
	    
		function cat_drop()
		{
		   $id = $this->uri->segment(3);
		   if($id)
		   {
		      $id = intval($id);
			  $this->db->where('id',$id)->delete('product_cat');
		   }
		}
		
		function cat_activate()
		{
		    $id = $this->uri->segment(3);
			if($id)
			{
			    $id = intval($id);
				$data = array('is_show'=>1);
				$this->db->where('id',$id)->update('product_cat',$data);
			}
			redirect('product/category');
		}
		
		function cat_batch_activate()
		{
		   $ids = $this->input->post('Cbselect');
		   if($ids)
		   {
		      for($i=0;$i<count($ids);$i++)
			  {
			       $id = intval($ids[$i]);
				   $data = array('is_show'=>1);
				   $this->db->where('id',$id)->update('product_cat',$data);
			  }
		   }
		}
		
		/*      
		  *   @function get category tree array
       		  @param  category id 
		 *    @return array
		*/
		function get_category_tree($id=0)
		{
		    $id = intval($id);
			$res = $this->db->where('parent_id',$id)->get('product_cat')->result_array();
			$tree_array = array();
			foreach($res as $row)
			{
			    $tree_array[$row['id']]['id'] = $row['id'];
				$tree_array[$row['id']]['cat_name'] = $row['cat_name'];
				if($row['parent_id'] != NULL)
				{
				    $tree_array[$row['id']]['child'] = $this->get_category_tree($row['id']);
				}
			}
			return $tree_array;
		}
		
		
	    
}